﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GOAP
{
    using GoapDataType = Dictionary<string, string>;
    using GoapActionType = Dictionary<string, Action>;

    abstract class Entity
    {
        public string name = "UnknownEntity";
        protected GoapDataType goapData;
        protected GoapActionType goapActions;
        public World world;

        //Avoid being instantiated directly
        protected Entity()
        {
            goapData = new GoapDataType();
            goapActions = new GoapActionType();
        }

        public void addAction(Action action)
        {
            goapActions.Add(action.name, action);
        }

        public List<Action> getPossibleActions()
        {
            List<Action> actionsCollection = new List<Action>();

            foreach (KeyValuePair<string, Action> pair in goapActions)
            {
                List<Action> actions = pair.Value.getActions(world, this);
                actionsCollection = actionsCollection.Concat(actions).ToList();
            }

            return actionsCollection;
        }

        public GoapDataType getGoapData()
        {
            return goapData;
        }

        public void print()
        {
            //Entity name
            System.Console.WriteLine(name);

            //Entity goapData
            if (goapData.Count > 0)
            {
                System.Console.WriteLine("  goapData:");
                foreach (KeyValuePair<string, string> pair in goapData)
                {
                    System.Console.WriteLine("    " + pair.Key + "\t" + pair.Value);
                }
            }

            if (goapActions.Count > 0)
            {
                //Entity actions
                System.Console.WriteLine("  goapActions:");
                foreach (KeyValuePair<string, Action> pair in goapActions)
                {
                    System.Console.WriteLine("    " + pair.Value.name);
                }
            }
        }

        virtual public void copyMembers(Entity copy)
        {
            //Copy Entity data to the copy
            copy.goapData = new GoapDataType(goapData);
            copy.goapActions = new GoapActionType();
            foreach (KeyValuePair<string, Action> pair in goapActions)
            {
                Action actcopy = pair.Value.clone();
                copy.goapActions.Add(actcopy.name, actcopy);
            }
            copy.name = name;
        }

        public abstract Entity clone(); 
    }
}
